package com.android.lyricshow.ui;

import java.util.ArrayList;

import com.android.lyricshow.R;
import com.android.lyricshow.adapter.AlbumAdapter;
import com.android.lyricshow.bean.AlbumBean;
import com.android.lyricshow.bean.SongBean;
import com.android.lyricshow.ui.ImageCache.ImageCacheParams;
import com.android.lyricshow.until.AppUntil;
import com.android.lyricshow.until.KEY;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class AlbumFragment extends BaseAlbumFragment implements OnClickListener{
	
	/**
	 * Album list
	 * */
	private ListView mAlbumListView			= null;
	
	private ImageWorker mImageFetcher		= null;

	private ArrayList<AlbumBean> mAlbumList	= null;
	
	private ArrayList<SongBean> mSonglist	= null;
		
	@SuppressWarnings("unchecked")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Bundle bd = this.getArguments();	
		if(bd != null){
			if(bd.containsKey(KEY.SONG_BEAN_LIST)){
				mSonglist = (ArrayList<SongBean>)bd.getSerializable(KEY.SONG_BEAN_LIST);
			}
			
			if(bd.containsKey(KEY.ALBUM_BEAN_LIST)){
				mAlbumList = (ArrayList<AlbumBean>)bd.getSerializable(KEY.ALBUM_BEAN_LIST);
			}
		}
		
		ImageCacheParams cacheParams = new ImageCacheParams();
		cacheParams.setMemCacheSizePercent(getActivity(), 0.25f);
		
		mImageFetcher = new ImageWorker(sContext, cacheParams);
		mImageFetcher.setDefaultImage(R.drawable.noalbum);
	}
	 
	@Override    
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		
		super.onCreateView(inflater, container, savedInstanceState);
		
		sFlipper.removeViewAt(1);
		
		// Album list
		mAlbumListView	= (ListView)sRootView.findViewById(R.id.all_list);
		mAlbumListView.setOnScrollListener(mScrollListener);
		mAlbumListView.setOnItemClickListener(mAlbumListListener);
		mAlbumListView.setAdapter(new AlbumAdapter(sContext, mImageFetcher, mAlbumList));
		
		// Album detail
		sAlbumBg.setOnClickListener(this);
		sBackBtn.setOnClickListener(this);
		sSettingBtn.setOnClickListener(this);
		
		sTitle.setText(R.string.main_album);
		
		return sRootView;
	}
	
	private OnItemClickListener mAlbumListListener = new OnItemClickListener(){

		@Override
		public void onItemClick(AdapterView<?> adapter, View view, int pos,
				long arg3) {
			
			// show album detail
			AppUntil.flipperShowNext(sContext, sFlipper);
			
			AlbumBean abean = (AlbumBean)adapter.getItemAtPosition(pos);		
			updateAlbumDetail(abean, mSonglist);
			
			sTitle.setText(abean.getName());
			sBackBtn.setVisibility(View.VISIBLE);
		}		
	};
	
	private OnScrollListener mScrollListener = new OnScrollListener(){

		@Override
		public void onScroll(AbsListView view, int firstVisibleItem,
				int visibleItemCount, int totalItemCount) {
		}

		@Override
		public void onScrollStateChanged(AbsListView view, int scrollState) {
			
			switch (scrollState) {
				case AbsListView.OnScrollListener.SCROLL_STATE_FLING:
					mImageFetcher.setPauseWork(true);
					break;
				case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
					int start	= mAlbumListView.getFirstVisiblePosition();
					int end 	= mAlbumListView.getLastVisiblePosition();
					
					mImageFetcher.setLoadLimit(start, end);
	                mImageFetcher.setPauseWork(false);
	                break;
				case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
					mImageFetcher.setPauseWork(true);
					break;
			}
		}
	};
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
			case R.id.title_bar_back:
				
				sTitle.setText(R.string.main_album);
				sBackBtn.setVisibility(View.INVISIBLE);
				
				AppUntil.flipperShowPrevious(sContext, sFlipper);		
				break;
			case R.id.ablum_view_bg:
				AppUntil.showToast(sContext, R.string.add_to_list);
				break;
			case R.id.title_bar_setting:
				Intent it = new Intent(sContext, PlayActivity.class);
				it.putExtra(KEY.NOW_PLAYING, 0);
				
				goToPlay(it);
				break;
		}
	}
	
	@Override
	public void onBackKey(){
		if(sFlipper.getDisplayedChild() == 0){
			this.getActivity().finish();
		}
		
		sTitle.setText(R.string.main_album);
		sBackBtn.setVisibility(View.INVISIBLE);
		
		AppUntil.flipperShowPrevious(sContext, sFlipper);	
	}
}
